#!/bin/bash

# 该脚本用于演示如何使用cfgo插件

# mysql安装目录
mysql_install_dir=/usr/local/mysql
mysql_config_file=/etc/my.cnf
mysql_data_dir=/data/mysql
mysql_data_backup_dir=/data/mysql_backup

# 检查依赖包是否安装
check_dependency() {
    echo "---- check dependency:"
    packages=("oeAware-manager" "oeAware-collector" "llvm-bolt")
    for package in "${packages[@]}"
    do
        if ! rpm -q $package &>/dev/null; then
            echo "ERROR: $package is not installed."
            exit 1
        else
            echo "$package is installed."
        fi
    done
    
    # sysboost未进版本，通过检查sysboostd可执行文件是否存在来判断
    if ! which sysboostd >/dev/null 2>&1; then
        echo "ERROR: sysboost is not installed."
        exit 1
    else
        echo "sysboost is installed."
    fi
    echo "---- check dependency success."
}

# 演示前清理环境
clean_env() {
    echo "---- clean env:"
    # 停止所有服务
    echo "stop mysql"
    pkill mysqld
    echo "stop oeaware"
    systemctl stop oeaware
    echo "stop sysboost"
    systemctl stop sysboost
    # 回退优化
    sysboostd --stop=${mysql_install_dir}/bin/mysqld
    echo "---- clean env success."
}

prepare_env() {
    echo "---- prepare env:"
    # 恢复备份数据
    if [ -d ${mysql_data_backup_dir} ]; then
        rm -rf ${mysql_data_dir}
        cp -r --reflink=never ${mysql_data_backup_dir} ${mysql_data_dir}
    else
        echo "ERROR: mysql data backup dir not exist."
        exit 1
    fi
    # 清理缓存
    echo 3 > /proc/sys/vm/drop_caches
    echo "---- prepare env success."
}

start_mysql() {
    echo "---- start mysql:"

    echo > ${mysql_data_dir}/log/mysql.log
    numactl -C 8-15 ${mysql_install_dir}/bin/mysqld --defaults-file=${mysql_config_file} &
    cnt=0
    while true; do
        log=`grep "ready for connections" ${mysql_data_dir}/log/mysql.log`
        if [ "$log" != "" ]; then
            log=`grep "ready for connections" ${mysql_data_dir}/log/mysql.log | grep -oE '\[Server\] (.+?): ready for connections\.'`
            break
        fi

        if [ "$cnt" = "600" ];then
            echo "ERROR: mysql start fail."
            exit 1
        fi

        sleep 1
        (( cnt++ ))
    done
    echo "---- start mysql success."
}

sysbench_test() {
    echo "---- sysbench test:"
    numactl -C 16-127 sysbench \
        --test=/usr/share/sysbench/tests/include/oltp_legacy/oltp.lua \
        --db-driver=mysql \
        --mysql-host=127.0.0.1 \
        --mysql-port=3306 \
        --mysql-user=root \
        --mysql-password=123456 \
        --oltp-range-size=100 \
        --oltp-point-selects=32 \
        --oltp-simple-ranges=1 \
        --oltp-sum-ranges=1 \
        --oltp-order-ranges=1 \
        --oltp-distinct-ranges=1 \
        --oltp-index-updates=1 \
        --oltp-non-index_updates=1 \
        --oltp-delete-inserts=1 \
        --mysql-db=sbtest \
        --oltp_table_size=10000000 \
        --tables=32 \
        --time=30  \
        --threads=64 \
        --percentile=95 \
        --report-interval=1 \
        run
    echo "---- sysbench test success."
}

# 演示

check_dependency
sleep 2
clean_env
sleep 2
prepare_env
sleep 2
start_mysql
sleep 2
sysbench_test